package com.app.aipass.app;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.app.aipass.activity.order.OrderRecordTabViewModel;
import com.app.aipass.data.DemoRepository;
import com.app.aipass.fragment.guadandetail.GuaDanDetailViewModel;
import com.app.aipass.fragment.main.FundViewModel;
import com.app.aipass.fragment.main.MyViewModel;
import com.app.aipass.fragment.main.TransactionViewModel;
import com.app.aipass.fragment.moneydetail.FundMoneyDetailViewModel;
import com.app.aipass.fragment.my.active.ActiveViewModel;
import com.app.aipass.fragment.my.exchangepoints.ExchangePointsViewModel;
import com.app.aipass.fragment.my.exchangepoints.exchangepointsrecord.ExchangePointsRecordViewModel;
import com.app.aipass.fragment.my.paytype.PayTypeViewModel;
import com.app.aipass.fragment.my.paytype.addpaytype.AddPayTypeViewModel;
import com.app.aipass.fragment.my.problem.ProblemDetailViewModel;
import com.app.aipass.fragment.my.reward.RewardViewModel;
import com.app.aipass.fragment.my.safe.SafeViewModel;
import com.app.aipass.fragment.my.set.SetViewModel;
import com.app.aipass.fragment.my.task.TaskTabViewModel;
import com.app.aipass.fragment.my.task.TaskViewModel;
import com.app.aipass.fragment.orderdetail.OrderDetailViewModel;
import com.app.aipass.viewmodel.LoginViewModel;
import com.app.aipass.viewmodel.TransactionTabViewModel;

/**
 * Created by goldze on 2019/3/26.
 */
public class AppViewModelFactory extends ViewModelProvider.NewInstanceFactory {
    @SuppressLint("StaticFieldLeak")
    private static volatile AppViewModelFactory INSTANCE;
    private final Application mApplication;
    private final DemoRepository mRepository;

    public static AppViewModelFactory getInstance(Application application) {
        if (INSTANCE == null) {
            synchronized (AppViewModelFactory.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AppViewModelFactory(application, Injection.provideDemoRepository());
                }
            }
        }
        return INSTANCE;
    }

    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }

    private AppViewModelFactory(Application application, DemoRepository repository) {
        this.mApplication = application;
        this.mRepository = repository;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(TransactionTabViewModel.class)) {
            return (T) new TransactionTabViewModel(mApplication, mRepository);
        }
        else if(modelClass.isAssignableFrom(TransactionViewModel.class))
        {
            return (T) new TransactionViewModel(mApplication, mRepository);
        }
        else if (modelClass.isAssignableFrom(OrderRecordTabViewModel.class)) {
            return (T) new OrderRecordTabViewModel(mApplication, mRepository);
        }
        else if(modelClass.isAssignableFrom(OrderDetailViewModel.class))
        {
            return (T) new OrderDetailViewModel(mApplication, mRepository);
        }
        else if(modelClass.isAssignableFrom(FundViewModel.class))
        {
            return (T) new FundViewModel(mApplication, mRepository);
        }
        else if(modelClass.isAssignableFrom(MyViewModel.class))
        {
            return (T) new MyViewModel(mApplication,mRepository);
        }
        else if(modelClass.isAssignableFrom(TaskViewModel.class))
        {
            return (T) new TaskViewModel(mApplication,mRepository);
        }
        else if(modelClass.isAssignableFrom(TaskTabViewModel.class))
        {
            return (T) new TaskTabViewModel(mApplication,mRepository);
        }
        else if(modelClass.isAssignableFrom(ExchangePointsViewModel.class))
        {
            return (T) new ExchangePointsViewModel(mApplication,mRepository);
        }
        else if(modelClass.isAssignableFrom(ExchangePointsRecordViewModel.class))
        {
            return (T) new ExchangePointsRecordViewModel(mApplication,mRepository);
        }
        else if(modelClass.isAssignableFrom(ActiveViewModel.class))
        {
            return (T) new ActiveViewModel(mApplication,mRepository);
        }
        else if(modelClass.isAssignableFrom(PayTypeViewModel.class))
        {
            return (T) new PayTypeViewModel(mApplication,mRepository);
        }
        else if(modelClass.isAssignableFrom(AddPayTypeViewModel.class))
        {
            return (T) new AddPayTypeViewModel(mApplication,mRepository);
        }
        else if(modelClass.isAssignableFrom(FundMoneyDetailViewModel.class))
        {
            return (T) new FundMoneyDetailViewModel(mApplication,mRepository);
        }
        else if(modelClass.isAssignableFrom(SetViewModel.class))
        {
            return (T) new SetViewModel(mApplication,mRepository);
        }
        else if(modelClass.isAssignableFrom(ProblemDetailViewModel.class))
        {
            return (T) new ProblemDetailViewModel(mApplication,mRepository);
        }
        else if(modelClass.isAssignableFrom(SafeViewModel.class))
        {
            return (T) new SafeViewModel(mApplication,mRepository);
        }
        else if(modelClass.isAssignableFrom(GuaDanDetailViewModel.class))
        {
            return (T) new GuaDanDetailViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(RewardViewModel.class)){
            return (T) new RewardViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(LoginViewModel.class)){
            return (T) new LoginViewModel(mApplication,mRepository);
        }

        throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
    }
}
